export const TIBETAN_COMPONENT_WEIGHTS = {
	'前加字': ['ག', 'ད', 'བ', 'མ', 'འ', ''],
	'上加字': ['ར', 'ལ', 'ས', '', 'ྲ', 'ླ'],
	'基字': [
		'ཀ', 'ཁ', 'ག', 'ང', 'ཅ', 'ཆ', 'ཇ', 'ཉ', 'ཏ', 'ཐ', 'ད', 'ན', 'པ', 'ཕ', 'བ', 'མ',
		'ཙ', 'ཚ', 'ཛ', 'ཝ', 'ཞ', 'ཟ', 'འ', 'ཡ', 'ར', 'ལ', 'ཤ', 'ས', 'ཧ', 'ཨ'
	],
	'下加字': ['ྱ', 'ྲ', 'ླ', 'ྭ', 'ྐྵ', 'ྤྵ', 'ྒྷ', ''],
	'元音': ['ི', 'ུ', 'ེ', 'ོ', 'ྀ', 'ཱུ', ''],
	'后加字': ['ག', 'ང', 'ད', 'ན', 'བ', 'མ', 'འ', 'ར', 'ལ', 'ས', ''],
	'再后加字': ['ད', 'ས', '']
};

const parseComponents = (syllable) => {
	const components = {
		前加字: '', 上加字: '', 基字: '', 下加字: '',
		元音: '', 后加字: '', 再后加字: ''
	};

	const structurePattern = 
		/^(?<pc>[གདབམའ]?)(?<uc>[རལསྲླ]?)(?<bc>[\u0F40-\u0F6C]+)(?<lc>[ྱྲླྭ]?)(?<vc>[\u0F72-\u0F87]?)(?<sc1>[གངདནབམའརལས]?)(?<sc2>[དས]?)$/;
	const match = syllable.match(structurePattern);
	
	if (match) {
		const groups = match.groups;
		components.前加字 = groups.pc || '';
		components.上加字 = groups.uc || '';
		components.基字 = groups.bc;
		components.下加字 = groups.lc || '';
		components.元音 = groups.vc || '';
		components.后加字 = groups.sc1 || '';
		components.再后加字 = groups.sc2 || '';
	}
	return components;
};

const compareComponent = (a, b, type) => {
	const weights = TIBETAN_COMPONENT_WEIGHTS[type];
	const indexA = weights.indexOf(a) !== -1 ? weights.indexOf(a) : weights.length;
	const indexB = weights.indexOf(b) !== -1 ? weights.indexOf(b) : weights.length;
	return indexA - indexB;
};

export function tibetanSort(data, keyword) {
	const sortField = 'བོད་ཡིག藏';
	const kw = keyword;

	data.sort((a, b) => {
		const aVal = a[sortField] || '';
		const bVal = b[sortField] || '';

		if (aVal === kw) return -1;
		if (bVal === kw) return 1;

		const tibetanPrefixMatch = (val) => val === kw || val.startsWith(kw + '་');
		const aPrefix = tibetanPrefixMatch(aVal);
		const bPrefix = tibetanPrefixMatch(bVal);
		if (aPrefix !== bPrefix) return aPrefix ? -1 : 1;

		const compA = parseComponents(aVal);
		const compB = parseComponents(bVal);

		const baseCompare = compareComponent(compA.基字, compB.基字, '基字');
		if (baseCompare !== 0) return baseCompare;

		const compareSequence = ['前加字', '上加字', '下加字', '元音', '后加字', '再后加字'];
		for (const component of compareSequence) {
			const compareResult = compareComponent(compA[component], compB[component], component);
			if (compareResult !== 0) return compareResult;
		}

		return aVal.length - bVal.length;
	});
}

export function generalSort(data, langType, keyword, sortField) {
	const kw = keyword;

	data.sort((a, b) => {
		const aVal = (a[sortField] || '').toString();
		const bVal = (b[sortField] || '').toString();

		if (aVal === kw && bVal !== kw) return -1;
		if (bVal === kw && aVal !== kw) return 1;

		const aPrefix = aVal.startsWith(kw);
		const bPrefix = bVal.startsWith(kw);
		if (aPrefix !== bPrefix) return aPrefix ? -1 : 1;

		const aInclude = aVal.includes(kw);
		const bInclude = bVal.includes(kw);
		if (aInclude !== bInclude) return aInclude ? -1 : 1;

		if (langType === '汉') {
			return aVal.localeCompare(bVal, 'zh-Hans-CN-u-co-pinyin');
		}
		return aVal.localeCompare(bVal);
	});
}